unit clsItem_Demonstrativo;

interface

uses Dm,dbtables,biblio;

type TItem_Demonstrativo = class (TObject)
  private
    Dem_codigo    :string;
    Det_Item      :string;
  public
    Hist_codigo   :string;
    Itd_Linha     :string;
    Itd_Acao      :string;
    Itd_Valor     :Double;
    X_historico   :string;
    function  Gravar(P_Dem_codigo,P_Det_Item,P_Hist_codigo,p_Itd_Linha:String):Byte;
    function  Recuperar(P_Dem_codigo,P_Det_Item,P_Hist_codigo,p_Itd_Linha:String):Byte;
    function  Recuperar_demo(P_Dem_codigo,P_Det_Item:String):Byte;
    function  Excluir(P_Dem_codigo,P_Det_Item,P_Hist_codigo,p_Itd_Linha:String):Byte;
    function  Excluir_Dem(P_Dem_codigo:String):Byte;
    procedure Consultar(var p_q:TQuery;P_Dem_codigo,P_Det_Item:String);
  end;


implementation

uses DB;

{ TItem_Demonstrativo }

procedure TItem_Demonstrativo.Consultar(var p_q: TQuery; P_Dem_codigo,
  P_Det_Item: String);
begin
  p_q.sql.clear;
  p_q.params.clear;
  p_q.SQL.add('Select * from Item_demonstrativo where Dem_codigo=:K_Dem_Codigo and Det_item=:K_det_item');
  p_q.ParamByName('K_Dem_codigo').AsString:= strzero(P_Dem_codigo,2);
  p_q.ParamByName('K_Det_item').AsString  := strzero(P_Det_Item,2);
end;

function TItem_Demonstrativo.Excluir(P_Dem_codigo, P_Det_Item,
  P_Hist_codigo,p_Itd_Linha: String): Byte;
var tq:TQuery;
begin
  tq:=Tquery.Create(dmint);
  tq.databasename:=dmint.db.databasename;
  tq.active:=False;
  tq.close;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('DELETE FROM Item_Demonstrativo where Dem_codigo=:k_Dem_Codigo '+
         'and Det_Item=:K_Det_Item and Hist_codigo=:K_Hist_codigo and Itd_Linha=:k_Itd_Linha');
  tq.parambyname('k_Dem_Codigo').asstring :=strzero(P_Dem_codigo,2);
  tq.parambyname('k_Det_Item').asstring:=strzero(P_Det_Item,2);
  tq.parambyname('k_Hist_codigo').asstring :=P_Hist_codigo;
  tq.parambyname('k_Itd_Linha').asstring   :=p_Itd_Linha;
  tq.execSQL;
  Result:=1;
  tq.close;
  tq.Free;
end;

function TItem_Demonstrativo.Excluir_dem(P_Dem_codigo: String): Byte;
var tq:TQuery;
begin
  tq:=Tquery.Create(dmint);
  tq.databasename:=dmint.db.databasename;
  tq.active:=False;
  tq.close;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('DELETE FROM Item_Demonstrativo where Dem_codigo=:k_Dem_Codigo');
  tq.parambyname('k_Dem_Codigo').asstring :=strzero(P_Dem_codigo,2);
  tq.execSQL;
  Result:=1;
  tq.close;
  tq.Free;
end;

function TItem_Demonstrativo.Gravar(P_Dem_codigo, P_Det_Item,
  P_Hist_codigo,p_Itd_Linha: String): Byte;
var tq:TQuery;
begin
  tq:=Tquery.Create(DmInt);
  tq.databasename:=dmint.Db.DatabaseName;
  tq.active:=False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('SELECT * FROM Item_Demonstrativo where Dem_codigo=:k_Dem_Codigo and '+
         ' Det_Item=:K_Det_Item and hist_codigo=:K_hist_codigo and Itd_Linha=:k_Itd_Linha');
  tq.parambyname('k_Dem_Codigo').asstring :=strzero(P_Dem_codigo,2);
  tq.parambyname('k_Det_Item').asstring:=strzero(P_Det_Item,2);
  tq.parambyname('k_Hist_codigo').asstring:=P_Hist_codigo;
  tq.parambyname('k_Itd_Linha').AsString  :=p_Itd_Linha;
  tq.open;
  if tq.eof then begin
    tq.sql.clear; tq.params.clear;
    tq.sql.add('INSERT INTO Item_Demonstrativo VALUES(:k_Dem_Codigo,'+
               ':K_Det_Item,:K_Hist_codigo,:K_Itd_Acao,:k_Itd_Linha,:k_Itd_Valor)');
  end else begin
    tq.sql.clear; tq.params.clear;
    tq.sql.add('UPDATE Item_Demonstrativo SET Itd_Acao=:k_Itd_Acao, Itd_Valor=:k_Itd_Valor WHERE '+
              'Dem_Codigo=:k_Dem_Codigo and Det_Item=:K_Det_Item and'+
              ' Hist_codigo=:K_Hist_codigo and Itd_Linha=:k_Itd_Linha');
  end;
  tq.parambyname('k_Dem_Codigo').asstring :=strzero(p_dem_Codigo,2);
  tq.parambyname('K_Det_Item').asstring   :=strzero(P_Det_Item,2);
  tq.parambyname('K_Hist_codigo').asstring:=P_Hist_codigo;
  tq.ParamByName('K_Itd_Acao').AsString   :=Itd_Acao;
  tq.ParamByName('k_Itd_Valor').AsFloat   := Itd_Valor;
  tq.ParamByName('k_Itd_Linha').AsString  := p_Itd_Linha;
  tq.close;
  tq.execsql;
  result:=1;
  tq.Free;
end;

function TItem_Demonstrativo.Recuperar(P_Dem_codigo, P_Det_Item,
  P_Hist_codigo,p_Itd_Linha: String): Byte;
var tq:TQuery;
begin
  tq:=Tquery.Create(dmint);
  tq.databasename:=DmInt.Db.DatabaseName;
  tq.active:=False;
  tq.sql.clear;
  tq.sql.add('SELECT * FROM Item_Demonstrativo where Dem_codigo=:k_Dem_Codigo and '+
         ' Det_Item=:K_Det_Item and Hist_codigo=:K_Hist_codigo and Itd_Linha=:k_Itd_Linha');
  tq.parambyname('k_Dem_Codigo').asstring :=strzero(P_Dem_codigo,2);
  tq.parambyname('k_Det_Item').asstring:=strzero(P_Det_Item,2);
  tq.parambyname('k_Hist_codigo').asstring :=P_Hist_codigo;
  tq.parambyname('k_Itd_Linha').AsString   := p_Itd_Linha;
  tq.open;
  dem_Codigo       :=p_dem_Codigo;
  Det_Item         :=P_Det_Item;
  Hist_codigo      :=P_Hist_codigo;
  Itd_Linha        := p_Itd_Linha;
  if tq.eof then begin
    Itd_Acao:='';
    Itd_Valor:= 0;
    Result:=0;
  end else begin
    Itd_Acao:=tq.fieldbyname('Itd_Acao').asstring;
    Itd_Valor:=tq.fieldbyname('Itd_Valor').AsFloat;
    Result:=1;
  end;
  tq.Close;
  tq.Free;
end;

function TItem_Demonstrativo.Recuperar_demo(P_Dem_codigo,
  P_Det_Item: String): Byte;
var tq:TQuery;
begin
  tq:=Tquery.Create(dmint);
  tq.databasename:=DmInt.Db.DatabaseName;
  tq.active:=False;
  tq.sql.clear;
  tq.sql.add('SELECT * FROM Item_Demonstrativo where Dem_codigo=:k_Dem_Codigo and '+
         ' Det_Item=:K_Det_Item');
  tq.parambyname('k_Dem_Codigo').asstring :=strzero(P_Dem_codigo,2);
  tq.parambyname('k_Det_Item').asstring:=strzero(P_Det_Item,2);
  tq.open;
  dem_Codigo       :=p_dem_Codigo;
  Det_Item         :=P_Det_Item;
  if tq.eof then begin
    Itd_Acao:='';
    Itd_Valor:=0;
    X_historico:='';
    Result:=0;
  end else begin
    Itd_Acao    :=tq.fieldbyname('Itd_Acao').asstring;
    Itd_Valor   :=tq.fieldbyname('Itd_Valor').AsFloat;
    X_historico :=tq.fieldbyname('hist_codigo').asstring;
    Itd_Linha   :=tq.fieldbyname('Itd_Linha').AsString;
    Result:=1;
  end;
  tq.Close;
  tq.Free;
end;

end.
